<?php

namespace app\admin\controller;

use app\common\model\Order as OrderModel;
use think\App;

class Order extends Base
{

    protected $order;

    public function __construct(App $app, OrderModel $order)
    {
        parent::__construct($app);
        $this->order = $order;
    }

    public function index(): string
    {
        if ($this->request->isAjax()) {
            $keyword = input('keyword');//昵称  订单号
            $type = input('type');
            $status = input('status');
            $pay_status = input('pay_status');

            $where = [];
            if ($keyword) $where[] = ['name|title', 'like', '%' . $keyword . '%'];
            if ($type) $where[] = ['type', '=', $type];
            if ($status) {
                if($status==2){
                    $where[] = ['status', 'in', [1,2]];
                }else{
                    $where[] = ['status', '=', $status];
                }
            }
            $query = $this->order->where($where);
            if ($pay_status == 1) $query->whereNull('pay_date');
            else if ($pay_status == 2) $query->whereNotNull('pay_date');
            $result = $query->order('id', 'desc')->paginate(10);
            ajaxSuccess($result);
        }
        return $this->fetch();
    }

    public function export()
    {
        $keyword = input('keyword');//昵称  订单号
        $shop_id = input('shop_id');
        $type = input('type');
        $stime = input('stime');
        $etime = input('etime');
        $wx_id = input('wx_id');
        $where[] = ['status', '=', 2];
        if ($keyword) $where[] = ['order_sn|nickname', 'like', '%' . $keyword . '%'];
        if ($shop_id == -1) $shop_id = 0;
        if ($shop_id) $where[] = ['shop_id', '=', $shop_id];
        if ($type) $where[] = ['type', '=', $type];
        if (!$stime) $stime = date('Y-m-01');
        if (!$etime) $etime = date('Y-m-d 23:59:59');
        if ($wx_id) $where[] = ['wx_id', '=', $wx_id];
        $where[] = ['order.create_time', 'between', [strtotime($stime), strtotime($etime)]];

        $list = $this->order->withJoin('user')->with(['wx', 'shop'])
            ->where($where)->order('id', 'desc')->select();
        $data = [['经销商', '昵称', '头像', '订单号', '类型', '性别', '支付时间', '支付金额', '获得/放入微信', '下单时间']];
        $typeArr = ['普通匹配', '精准匹配', '放入微信', '摆摊赚钱'];
        $sex = ['', '男', '女'];
        foreach ($list as $value) {
            $data[] = [
                $value['shop_id'] > 0 ? $value['shop']['nickname'] : '平台',
                $value['user']['nickname'],
                $value['user']['avatar'],
                $value['order_sn'],
                $typeArr[intval($value['type']) - 1],
                $sex[$value['sex']],
                $value['pay_time'],
                $value['price'] == 0 ? '首次放入免费' : $value['price'],
                $value['wx']['number'],
                $value['create_time']
            ];
        }
        exportSvc($data, '订单列表');

    }

    public function save(\app\index\validate\Order $order)
    {
        $data = $this->request->post(['id' => '', 'title' => '', 'name' => '', 'type' => 1, 'idcard' => '', 'mobile' => '']);
        if (!$order->check($data)) ajaxError($order->getError());
        if (!$data['id']) ajaxError('参数不足');
        $this->order->where('id', $data['id'])->update($data);
        ajaxSuccess();
    }

    public function delete()
    {
        $id = input('post.id');
        $this->order::destroy($id);
        ajaxSuccess();
    }

    public function success()
    {
        $id = input('post.id');
        $order = $this->order->where('id', $id)->find();
        if (!$order) ajaxError('订单不存在');
        if ($order['status'] == 3) ajaxError('已被设为成功');
        $remark = $order['remark'];
        $remark[] = [
            'title' => '设为成功', 'date' => date('Y-m-d H:i:s')
        ];
        $order->remark = $remark;
        $order->status = 3;
        $order->save();
        ajaxSuccess();
    }

    public function error()
    {
        $id = input('post.id');
        $order = $this->order->where('id', $id)->find();
        if (!$order) ajaxError('订单不存在');
        if ($order['status'] == 4) ajaxError('已被设为失败');
        $remark = $order['remark'];
        $remark[] = [
            'title' => '设为失败', 'date' => date('Y-m-d H:i:s')
        ];
        $order->remark = $remark;
        $order->status = 4;
        $order->save();
        ajaxSuccess();
    }

}

